/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var curAutoReplyId = false;

var ZXXFILEPD1 = false;

$(function() {
    Loading.finish();
});


function c7tab2onload() {
    // 加载横幅数据
    $.get(shoproot + '?/WdminAjax/getBannerData', function(r) {
        var i = 0;
        r = r.toJson();
        for (i in r) {
            var n = $('#cat' + r[i].relid + 'img');
            $('#cat' + r[i].relid + 'img-href').val(r[i].banner_href);
            n.attr('src', shoproot + 'static/banner/' + r[i].banner_image);
            n.parent().attr('href', shoproot + 'static/banner/' + r[i].banner_image);
        }
        $.jUploader({
            button: 'cat0-up',
            action: shoproot + '?/WdminAjax/BannerImageUpload/',
            onComplete: function(fileName, response) {
                var R = response;
                if (R.s > 0) {
                    Alert('图片上传成功');
                    $('#cat0img').attr('src', shoproot + 'static/banner/' + R.imgn);
                    BannerDataSave(0, R.imgn);
                } else {
                    Alert('上传图片失败，请联系技术支持');
                }
            }
        });
        $.jUploader({
            button: 'cat1-up',
            action: shoproot + '?/WdminAjax/BannerImageUpload/',
            onComplete: function(fileName, response) {
                var R = response;
                if (R.s > 0) {
                    Alert('图片上传成功');
                    $('#cat1img').attr('src', shoproot + 'static/banner/' + R.imgn);
                    BannerDataSave(1, R.imgn);
                } else {
                    Alert('上传图片失败，请联系技术支持');
                }
            }
        });
        $.jUploader({
            button: 'cat2-up',
            action: shoproot + '?/WdminAjax/BannerImageUpload/',
            onComplete: function(fileName, response) {
                var R = response;
                if (R.s > 0) {
                    Alert('图片上传成功');
                    $('#cat2img').attr('src', shoproot + 'static/banner/' + R.imgn);
                    BannerDataSave(2, R.imgn);
                } else {
                    Alert('上传图片失败，请联系技术支持');
                }
            }
        });
        $.jUploader({
            button: 'cat3-up',
            action: shoproot + '?/WdminAjax/BannerImageUpload/',
            onComplete: function(fileName, response) {
                var R = response;
                if (R.s > 0) {
                    Alert('图片上传成功');
                    $('#cat3img').attr('src', shoproot + 'static/banner/' + R.imgn);
                    BannerDataSave(3, R.imgn);
                } else {
                    Alert('上传图片失败，请联系技术支持');
                }
            }
        });
    });
}

function BannerDataSave(rel, imgn) {
    $.post(shoproot + '?/WdminAjax/SaveBannerData/', {
        relid: rel,
        href: $('#cat' + rel + 'img-href').val(),
        img: imgn
    }, function(R) {
        debug(R);
    });
}

function c7tab1onload() {
    loadSettings();
    loadAutoReplys();

    $(".addAutoReplyKey").fancybox({
        openEffect: 'fade',
        closeEffect: 'elastic',
        openSpeed: 'fast',
        closeSpeed: 'fast',
        overlayColor: '#fff',
        scrolling: false
    });

    $('#selectgmess').fancybox({
        openEffect: 'fade',
        closeEffect: 'elastic',
        openSpeed: 'fast',
        closeSpeed: 'fast',
        overlayColor: '#fff'
    });
}

function bindMsg(rel) {
    // curAutoReplyId
    // rel
    $.fancybox.close();
    $.post(shoproot + '?/WdminAjax/updateAutoReply/', {
        rel: rel,
        id: curAutoReplyId1
    }, function(r) {
        if (r > 0) {
            Alert('修改成功');
            $('#_gmess_').load(shoproot + '?/WdminAjax/ajaxGmess/id=' + rel);
        } else {
            Alert('修改失败', 1);
        }
    });
}

/**
 * 系统设置保存
 * @returns {undefined}
 */
function set1Save() {
    $.post('?/WdminAjax/setSettings/', {
        shop_name: $('#set-form-title').val(),
        wechat_subscribe_welcome: $('#set-form-subscribe-text').val(),
        company_profit_percent: $('#set-form-proxypoint').val()
    }, function(r) {
        if (parseInt(r) > 0) {
            Alert('保存成功');
        } else {
            Alert('保存失败');
        }
    });
}

/**
 * 系统设置保存
 * @returns {undefined}
 */
function set2Save() {
    $.post('?/WdminAjax/setAutoReplys/', {
        data: autoReplys
    }, function(r) {
        if (parseInt(r) > 0) {
            Alert('保存成功');
        } else {
            Alert('保存失败');
        }
    });
}

/**
 * 
 * @returns {undefined}
 */
function loadSettings() {
    $.get(shoproot + '?/WdminAjax/ajaxLoadSettings', function(r) {
        r = r.toJson();
        $('#set-form-title').val(r.shop_name);
        $('#set-form-subscribe-text').html(r.wechat_subscribe_welcome);
        $('#set-form-proxypoint').val(r.company_profit_percent);
    });
}

/**
 * 
 * @returns {undefined}
 */
function loadAutoReplys() {
    $.get(shoproot + '?/WdminAjax/getAutoReplys/', function(r) {
        // debug(r);
        r = r.toJson();
        window.autoReplys = r;
        var str = '';
        for (var rp in r) {
            r[rp].id = parseInt(r[rp].id);
            str += '<li><a href="javascript:;" class="autoReplyLi" rel-id="' + r[rp].id + '" rel-data="' + r[rp].rel + '"><i class="del" rel-data="' + r[rp].id + '"></i>' + r[rp].key + '</a></li>';
        }
        $('.key-reply-ul').html(str);
        $('.autoReplyLi').click(ARpsClick).eq(0).click();
        $('.autoReplyLi').find('i.del').click(AutoReplyDel);
    });
}

/**
 * 
 * @returns {undefined}
 */
function ARpsClick() {
    $('.autoReplyLi.click').removeClass('click');
    $(this).addClass('click');
    var relData = parseInt($(this).attr('rel-data'));
    $('#_gmess_').load(shoproot + '?/WdminAjax/ajaxGmess/id=' + relData);
    // $('#set-form-autoReply').val(AutoReplyGet(relData)).focus();
    window.curAutoReplyId = relData;
    window.curAutoReplyId1 = parseInt($(this).attr('rel-id'));
}

/**
 * 
 * @param {type} id
 * @param {type} message
 * @returns {undefined}
 */
function AutoReplySet(id, message) {
    if (!id)
        return id;
    var _t;
    for (_t in autoReplys) {
        if (autoReplys[_t].id === id) {
            autoReplys[_t].message = message;
        }
    }
}

/**
 * 
 * @param {type} id
 * @returns {undefined}
 */
function AutoReplyGet(id) {
    var _t;
    for (_t in autoReplys) {
        if (autoReplys[_t].id === id) {
            return autoReplys[_t].message;
        }
    }
}

/**
 * 
 * @param {type} message
 * @returns {undefined}
 */
function autoReplyAutoSave(message) {
    AutoReplySet(curAutoReplyId, message);
}

/**
 * 删除
 */
function AutoReplyDel() {
    curAutoReplyId = parseInt($(this).attr('rel-data'));
    var btn = $(this).parent().parent();
    if (confirm('你确定要删除这个关键字吗')) {
        $.post('?/WdminAjax/deleteAutoReplys/', {
            id: curAutoReplyId
        }, function(r) {
            if (parseInt(r) > 0) {
                btn.slideUp();
                $('#set-form-autoReply').val('');
            } else {
                Alert('删除失败');
            }
        });
    }
}

function AutoRepKeySave() {
    var key = $('#set-form-key1').val();
    if (key !== '') {
        $.post('?/WdminAjax/addAutoReplys/', {
            key: key
        }, function(r) {
            if (parseInt(r) > 0) {
                $('#set-form-key1').val('');
                loadAutoReplys();
                $.fancybox.close();
            } else {
                Alert('添加失败');
            }
        });
    }
}